package com.putao.album.child.crop;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.putao.album.R;
import com.putao.album.base.BaseActivity;
import com.putao.album.eventbus.BasePostEvent;
import com.putao.album.util.AppHelper;
import com.putao.album.util.BitmapHelper;
import com.putao.album.util.CommonUtils;
import com.putao.album.util.DisplayHelper;
import com.putao.album.util.PuTaoConstants;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import de.greenrobot.event.EventBus;

/**
 * Created by chen on 15-5-27.
 */
public class ActivityCropImage extends BaseActivity {
    private CropView mCropView;
    private String filePath;

    @Override
    public int doGetContentViewId() {
        return R.layout.activity_crop_image;
    }

    @Override
    public void doInitSubViews(View view) {
        TextView title_tv = (TextView) findViewById(R.id.title_tv);
        title_tv.setText("裁切");
        Button right_btn = (Button) findViewById(R.id.right_btn);
        right_btn.setText(getString(R.string.finished));


        addOnClickListener(R.id.right_btn, R.id.back_btn);

        mCropView = (CropView) findViewById(R.id.cropView);

    }

    @Override
    public void doInitData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            filePath = bundle.getString("photo_path");
            Bitmap bitmap = BitmapHelper.getInstance().getBitmapFromPathWithSize(filePath, DisplayHelper.getScreenWidth(), DisplayHelper.getScreenHeight());
            int ori = CommonUtils.getImageFileOrientation(filePath);
            if (ori != ExifInterface.ORIENTATION_NORMAL) {
                bitmap = CommonUtils.orientBitmap(bitmap, ori);
            }
            mCropView.setImageBitmap(bitmap);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.right_btn:
                saveImage();
                finish();
                break;
            default:
                break;
        }

    }

    private void saveImage() {
        try {
            int desW=500;
            int desH=500;
            File file = new File(AppHelper.getAppDiskDir(),".tmp");
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            Bitmap corpImage = mCropView.clip();
            Matrix matrix = new Matrix();
            float scalex=(float) desW/corpImage.getWidth();
            float scaley=(float) desH/corpImage.getHeight();
            matrix.postScale(scalex,scaley);
            corpImage = Bitmap.createBitmap(corpImage, 0, 0, corpImage.getWidth(), corpImage.getHeight(),matrix,false);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            corpImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            bos.write(baos.toByteArray());
            bos.flush();
            fos.getFD().sync();
            bos.close();

            Bundle bundle = new Bundle();
            bundle.putString("photo_path", file.getPath());
            EventBus.getDefault().post(new BasePostEvent(PuTaoConstants.EVENTBUS_FINISH_PHOTO_SELECT, bundle));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
